import axios from 'axios';
import type { CommonRes } from './commonType'
import { isUndefined, isNull } from 'lodash';


export interface CluePageQuery{
  page: number,
  size: number,
  start_time: string,
  end_time: string,
  organization_personnel_uid?: number,
  phone_viewed?: number,
  mobile?: number,
  plan?: string
}


export interface ClueHandedOverEntity{
  id: number,
  uid: number,
  remark?: string
}

export interface ClueItem{
  id: number,
  uid: number,
  age: number,
  gender: number, //性别 (1: 男, 2: 女, 3: 未知)
  city: string,
  province: string,
  name: string,
  mobile: string,
  work: string,
  channel_name: string,
  organization_name: string,
  plan: string,     // 沟通进度
  nickname: string, // 跟进经理
  status: string,
  baitiao_quota: string, //白条额度
  huabei_quota: string, /// 花呗额度
  remark: string   // 跟进备注
  identity: string   //身份证 (stored in UTF-16)
  district: string,  // 区
  credit_status?: string, // 信用状态
  product_name: string, // 产品名称
  product_id: number,  // 产品ID
  region: string,       //居住城市
  sesame_score: number, //芝麻分
  insurance: number, // 保单(1: 有, 0: 无)
  car: number,   //车子 (1: 有, 0: 无)
  house: number, //房子 (1: 有, 0: 无)
  stage: string, //阶段 ('applying', 'data_filled', 'application_submitted', 'application_fail')
  social_security: number, //社保 (1: 有, 0: 无)
  accumulation_fund: number, //公积金 (1: 有, 0: 无)
  ip: string, //地址
  phone_viewed: number, //手机是否可见
  update_num: any,   //更新次数
  createtime: any,   //创建时间
  deletetime: any,   //删除时间
  updatetime: any,        //更新时间
  business_owner: number  //企业主--营业执照 (1: 有, 0: 无)
  loan_money: number  //贷款的金额

  // mobile_md - 手机号 (MD5 hash)
  // mobile - 手机号
  // name - 真实名字
  // gender - 
  // age - 年龄
  // identity - 身份证 (stored in UTF-16)
  // region - 居住城市
  // province - 省份
  // city - 市
  // district - 区
  // sesame_score - 芝麻分
  // huabei_quota - 花呗额度
  // baitiao_quota - 白条额度
  // work - 工作内容
  // car - 车子 (1: 有, 0: 无)
  // house - 房子 (1: 有, 0: 无)
  // accumulation_fund - 公积金 (1: 有, 0: 无)
  // social_security - 
  // insurance - 保单 (1: 有, 0: 无)
  // business_owner - 营业执照 (1: 有, 0: 无)
  // loan_money - 贷款的金额
  // ip - IP 地址
  // stage - 阶段 ('applying', 'data_filled', 'application_submitted', 'application_fail')
  // update_num - 更新次数
  // createtime - 创建时间
  // deletetime - 删除时间
  // updatetime - 更新时间
}

export interface  ClueBehaviorReportDetail {
  id: number, // 主键ID
  name: string, // 名字
  identity: string, // 身份证
  loan_behavior_score:string,  // 贷款行为分
  loan_count_1_month:string,   // 近1个月贷款笔数
  loan_count_3_months:string,  // 近3个月贷款笔数
  loan_count_6_months:string,  // 近6个月贷款笔数
  loan_count_12_months:string, // 近12个月贷款笔数
  loan_count_24_months:string, // 近24个月贷款笔数
  loan_amount_1_month:string,  // 近1个月贷款总金额参考金额区间
  loan_amount_3_months:string, // 近3个月贷款总金额参考金额区间
  loan_amount_6_months:string, // 近6个月贷款总金额参考金额区间
  loan_amount_12_months:string, // 近12个月贷款总金额参考金额区间
  loan_amount_24_months:string, // 近24个月贷款总金额参考金额区间
  loan_amount_1k_or_below_12_months:string, // 近12个月贷款金额在1k及以下的笔数
  loan_amount_1k_3k_12_months:string, // 近12个月贷款金额在1k-3k的笔数
  loan_amount_3k_10k_12_months:string, // 近12个月贷款金额在3k-10k的笔数
  loan_amount_above_10k_12_months:string, // 近12个月贷款金额在1w以上的笔数
  loan_org_count_1_month:string, // 近1个月贷款机构数
  loan_org_count_3_months:string, // 近3个月贷款机构数
  loan_org_count_6_months:string, // 近6个月贷款机构数
  loan_org_count_12_months:string, // 近12个月贷款机构数
  loan_org_count_24_months:string, // 近24个月贷款机构数
  loan_financial_org_count_12_months:string, // 近12个月消金类贷款机构数
  loan_financial_org_count_24_months:string, // 近24个月消金类贷款机构数
  loan_online_org_count_12_months:string, // 近12个月网贷类贷款机构数
  loan_online_org_count_24_months:string, // 近24个月网贷类贷款机构数
  overdue_loan_count_m0_6m:string, // 近6个月M0+逾期贷款笔数
  overdue_loan_count_m0_12m:string, // 近12个月M0+逾期贷款笔数
  overdue_loan_count_m0_24m:string, // 近24个月M0+逾期贷款笔数
  overdue_loan_count_m1_6m:string, // 近6个月M1+逾期贷款笔数
  overdue_loan_count_m1_12m:string, // 近12个月M1+逾期贷款笔数
  overdue_loan_count_m1_24m:string, // 近24个月M1+逾期贷款笔数
  cumulative_overdue_amount_6m: string, // 近6个月累计逾期金额
  cumulative_overdue_amount_12m: string, // 近12个月累计逾期金额
  cumulative_overdue_amount_24m: string, // 近24个月累计逾期金额
  normal_repayment_ratio: string, // 正常还款订单数占贷款总订单数比例
  failed_deduction_count_1m: string, // 近1个月失败扣款笔数
  failed_deduction_count_3m: string, // 近3个月失败扣款笔数
  failed_deduction_count_6m: string, // 近6个月失败扣款笔数
  failed_deduction_count_12m: string, // 近12个月失败扣款笔数
  failed_deduction_count_24m: string, // 近24个月失败扣款笔数
  loan_performance_amount_1m: string, // 近1个月履约贷款总金额
  loan_performance_amount_3m: string, // 近3个月履约贷款总金额
  loan_performance_amount_6m: string, // 近6个月履约贷款总金额
  loan_performance_amount_12m: string, // 近12个月履约贷款总金额
  loan_performance_amount_24m: string, // 近24个月履约贷款总金额
  loan_performance_count_1m: string, // 近1个月履约贷款次数
  loan_performance_count_3m: string, // 近3个月履约贷款次数
  loan_performance_count_6m: string, // 近6个月履约贷款次数
  loan_performance_count_12m: string, // 近12个月履约贷款次数
  loan_performance_count_24m: string, // 近24个月履约贷款次数
  last_performance_days: string, // 最近一次履约距今天数
  loan_behavior_confidence: string, // 贷款行为置信度
  loan_settled_orders: string, // 贷款已结清订单数
  credit_loan_duration: string, // 信用贷款时长
  last_loan_disbursement_time: string, // 最近一次贷款放款时间

}


/**
 * 分页获取
 * @param data 
 * @returns 
 */
export function getCluePageList(postData: CluePageQuery) {
  return axios.post<CommonRes>('/organization/clue/listpage', postData).then(({ data })=>{
    if(!isUndefined(data?.datalist?.data) 
      && !isNull(data?.datalist?.data)){
      let arrList : ClueItem[] = [];
      data?.datalist?.data.forEach((item: any) => {
        arrList.push({
          id: item?.id,
          uid: item?.uid,
          age: item?.clue?.age,
          gender: item?.clue?.gender,
          city: item?.clue?.city,
          district: item?.clue?.district,
          province: item?.clue?.province,
          name: item?.clue?.name,
          mobile: item?.clue?.mobile,
          work: item?.clue?.work,
          channel_name: item?.channel_name,
          identity: item?.clue?.identity,
          organization_name: '',
          plan: item?.plan,
          product_name: item?.product?.name,
          product_id: item?.product?.id,
          loan_money: item?.clue?.loan_money,
          phone_viewed: item?.phone_viewed,
          status: '',
          remark: item?.remark,
          credit_status: item?.clue?.credit_status,
          region: item?.clue?.region,
          sesame_score: item?.clue?.sesame_score,
          car: item?.clue?.car,
          house: item?.clue?.house,
          stage: item?.clue?.stage,
          nickname: item?.organization_personnel?.nickname,
          social_security: item?.clue?.social_security,
          accumulation_fund: item?.clue?.accumulation_fund,
          ip: '',
          baitiao_quota: item?.clue?.baitiao_quota,
          update_num: item?.clue?.update_num,
          createtime: item?.createtime,
          deletetime: '',
          updatetime: '',
          business_owner: item?.clue?.business_owner,
          insurance: item?.clue?.insurance,
          huabei_quota: item?.clue?.huabei_quota,
        })
      })
      return { 
        data: arrList, 
        total: data?.datalist?.total || 0
      }
    }
    return { data: [], total: 0 }
  });
}


/**
 * 移交
 * @param data 
 * @returns 
 */
export function clueHandedOver(data: ClueHandedOverEntity) {
  return axios.post<CommonRes>('/organization/clue/update', data);
}

/**
 * 获取人员列表
 * @returns 
 */
export function getClueSlectList() {
  return axios.post<CommonRes>('/organization/Personnel/select');
}



/**
 * 更新沟通进度
 * @param data 
 * @returns 
 */
export function updateCommProgress(data: { 
  id: number, 
  uid: number, 
  plan: string, 
  remark?: string
}) {
  return axios.post<CommonRes>('/organization/clue/update', data);
}


/**
 * 手机号查看
 * @param data 
 * @returns 
 */
export function cluesPhoneViewed(id: number) {
  return axios.post<CommonRes>('/organization/clue/phoneViewed', { id });
}
/**
 * 获取详情
 * @param data 
 * @returns 
 */
export function getCluesDtails(id: string) {
  return axios.post<CommonRes>('/organization/clue/details', { id }).then(({ data })=>{
    let userInfo: ClueItem = null;
    if(!isNull(data?.details)){
      let item = data?.details || null;
      userInfo = {
        id: item?.id,
        uid: item?.uid,
        age: item?.clue?.age,
        gender: item?.clue?.gender,
        city: item?.clue?.city,
        district: item?.clue?.district,
        province: item?.clue?.province,
        name: item?.clue?.name,
        mobile: item?.clue?.mobile,
        work: item?.clue?.work,
        channel_name: item?.channel_name,
        identity: item?.clue?.identity,
        organization_name: '',
        plan: item?.plan,
        product_name: item?.product?.name,
        product_id: item?.product?.id,
        loan_money: item?.clue?.loan_money,
        phone_viewed: item?.phone_viewed,
        status: item?.status,
        remark: item?.remark,
        region: item?.clue?.region,
        credit_status: item?.clue?.credit_status,
        sesame_score: item?.clue?.sesame_score,
        car: item?.clue?.car,
        house: item?.clue?.house,
        stage: item?.clue?.stage,
        nickname: item?.organization_personnel?.nickname,
        social_security: item?.clue?.social_security,
        accumulation_fund: item?.clue?.accumulation_fund,
        ip: '',
        baitiao_quota: item?.clue?.baitiao_quota,
        update_num: item?.clue?.update_num,
        createtime: item?.createtime,
        deletetime: '',
        updatetime: '',
        insurance: item?.clue?.insurance,
        business_owner: item?.clue?.business_owner,
        huabei_quota: item?.clue?.huabei_quota,
      }


      return { userInfo, clueReportDetail: item?.clueBehaviorReportDetail }
    }

    return null;
  });
}
